GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 23c905...137f54 )
by Florian
01:14
created

App.parseMarkersFromCookies   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 49
rs 9.2258

2 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 3 1
D 0 37 9
1
/*jslint
2
  indent: 4
3
*/
4
5
/*global
6
  $, google, Lang, Lines, Markers, Conversion, Cookies, Coordinates, trackMarker, showAlert,
7
  id2alpha, alpha2id,
8
  showLinkDialog,
9
  osmProvider, osmDeProvider, thunderforestProvider, opentopomapProvider,
10
  get_cookie_int, get_cookie_float, get_cookie_string,
11
  Attribution, Sidebar, ExternalLinks, Hillshading, Geolocation, NPA, CDDA, Freifunk, Okapi,
12
  DownloadGPX, API_KEY_THUNDERFOREST,
13
  restoreCoordinatesFormat,
14
  document, window
15
*/
16
17
18
var CLAT_DEFAULT = 51.163375;
19
var CLON_DEFAULT = 10.447683;
20
var ZOOM_DEFAULT = 12;
21
var ZOOM_DEFAULT_GEOCACHE = 15;
22
var MAPTYPE_DEFAULT = "OSM";
23
24
25
var App = {};
26
App.m_map = null;
27
28
App.urlParams = function () {
29
    'use strict';
30
31
    var params = {},
32
        splitted = window.location.search.substr(1).split('&'),
33
        i,
34
        p;
35
36
    for (i = 0; i < splitted.length; i += 1) {
37
        p = splitted[i].split('=', 2);
38
        if (p[0] !== "") {
39
            if (p.length === 1) {
40
                params[p[0]] = "";
41
            } else {
42
                params[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
43
            }
44
        }
45
    }
46
47
    return params;
48
};
49
50
51
App.init = function () {
52
    'use strict';
53
54
    Lang.init();
55
56
    var params = App.urlParams();
57
    if (!App.initFromUrl(params)) {
58
        App.initFromCookies();
59
    }
60
};
61
62
63
App.initFromUrl = function (params) {
64
    'use strict';
65
66
    if (params.c === undefined &&
67
            params.z === undefined &&
68
            params.t === undefined &&
69
            params.m === undefined &&
70
            params.d === undefined &&
71
            params.f === undefined &&
72
            params.g === undefined) {
73
        return false;
74
    }
75
76
    var center = this.parseCenterFromUrl(params.c),
77
        zoom = this.repairZoom(parseInt(params.z, 10), (params.g === undefined) ? ZOOM_DEFAULT : ZOOM_DEFAULT_GEOCACHE),
78
        maptype = this.repairMaptype(params.t, MAPTYPE_DEFAULT),
79
        markerdata = this.parseMarkersFromUrl(params.m),
80
        defaultCenter = false;
81
82
    if (!center && markerdata.length > 0) {
83
        var clat = 0,
84
            clng = 0;
85
        markerdata.map(function (m) {
86
            clat += m.coords.lat();
87
            clng += m.coords.lng();
88
        });
89
        center = new google.maps.LatLng(clat / markerdata.length, clng / markerdata.length);
90
    }
91
    if (!center) {
92
        center = new google.maps.LatLng(CLAT_DEFAULT, CLON_DEFAULT);
93
        defaultCenter = true;
94
    }
95
96
    App.m_map = this.createMap("themap", center, zoom, maptype);
97
98
    markerdata.map(function (m) {
99
        Markers.newMarker(m.coords, m.id, m.r, m.name, m.color);
100
    });
101
102
    this.parseLinesFromUrl(params.d).map(function (m) {
103
        Lines.newLine(m.source, m.target);
104
    });
105
106
    App.restore(params.f, params.g);
107
108
    if (defaultCenter && params.g !== undefined) {
109
        Geolocation.whereAmI();
110
    }
111
112
    return true;
113
};
114
115
116
App.initFromCookies = function () {
117
    'use strict';
118
119
    var clat = this.repairLat(get_cookie_float('clat', CLAT_DEFAULT)),
120
        clon = this.repairLon(get_cookie_float('clon', CLON_DEFAULT)),
121
        defaultCenter = (clat === CLAT_DEFAULT && clon === CLON_DEFAULT),
122
        center = new google.maps.LatLng(clat, clon),
123
        zoom = this.repairZoom(get_cookie_int('zoom', ZOOM_DEFAULT), ZOOM_DEFAULT),
124
        maptype = this.repairMaptype(get_cookie_string('maptype', MAPTYPE_DEFAULT), MAPTYPE_DEFAULT);
125
126
127
    App.m_map = this.createMap("themap", center, zoom, maptype);
128
129
    this.parseMarkersFromCookies().map(function (m) {
130
        Markers.newMarker(m.coords, m.id, m.r, m.name, m.color);
131
    });
132
133
    this.parseLinesFromCookies().map(function (m) {
134
        Lines.newLine(m.source, m.target);
135
    });
136
137
    App.restore(undefined);
138
139
    if (defaultCenter) {
140
        Geolocation.whereAmI();
141
    }
142
};
143
144
145
App.restore = function (features, geocache) {
146
    'use strict';
147
148
    if (geocache !== undefined) {
149
        Okapi.setShowCache(geocache);
150
    }
151
152
    Sidebar.restore(true);
153
154
    if (features === undefined) {
155
        Hillshading.restore(false);
156
        Okapi.restore(false);
157
        NPA.toggle(false);
158
        CDDA.toggle(false);
159
        Freifunk.toggle(false);
160
    } else {
161
        features = features.toLowerCase();
162
        Hillshading.toggle(features.indexOf('h') >= 0);
163
        Okapi.toggle(features.indexOf('g') >= 0);
164
        NPA.toggle(features.indexOf('n') >= 0);
165
        Freifunk.toggle(features.indexOf('f') >= 0);
166
    }
167
168
    restoreCoordinatesFormat("DM");
169
170
    if (geocache !== undefined) {
171
        Okapi.toggle(true);
172
    }
173
};
174
175
176
App.storeCenter = function () {
177
    'use strict';
178
179
    var c = App.m_map.getCenter();
180
    Cookies.set('clat', c.lat(), {expires: 30});
181
    Cookies.set('clon', c.lng(), {expires: 30});
182
};
183
184
185
App.storeZoom = function () {
186
    'use strict';
187
188
    Cookies.set('zoom', App.m_map.getZoom(), {expires: 30});
189
};
190
191
192
App.storeMapType = function () {
193
    'use strict';
194
195
    Cookies.set('maptype', App.m_map.getMapTypeId(), {expires: 30});
196
};
197
198
199
App.getFeaturesString = function () {
200
    'use strict';
201
202
    var s = "";
203
    if ($('#geocaches').is(':checked')) {
204
        s += "g";
205
    }
206
    if ($('#hillshading').is(':checked')) {
207
        s += "h";
208
    }
209
    if ($('#npa').is(':checked')) {
210
        s += "n";
211
    }
212
    if ($('#freifunk').is(':checked')) {
213
        s += "f";
214
    }
215
216
    return s;
217
};
218
219
220
App.getPermalink = function () {
221
    'use strict';
222
223
    var lat = App.m_map.getCenter().lat(),
224
        lng = App.m_map.getCenter().lng(),
225
        geocache = Okapi.popupCacheCode(),
226
        url = "https://flopp.net/" +
227
                "?c=" + lat.toFixed(6) + ":" + lng.toFixed(6) +
228
                "&z=" + App.m_map.getZoom() +
229
                "&t=" + App.m_map.getMapTypeId() +
230
                "&f=" + this.getFeaturesString() +
231
                "&m=" + Markers.toString() +
232
                "&d=" + Lines.getLinesText();
233
    if (geocache) {
234
        url += "&g=" + geocache;
235
    }
236
    return url;
237
};
238
239
App.generatePermalink = function () {
240
    'use strict';
241
242
    var link = this.getPermalink();
243
    showLinkDialog(link);
244
};
245
246
247
App.repairLat = function (x, d) {
248
    'use strict';
249
250
    if (Coordinates.validLat(x)) {
251
        return x;
252
    }
253
254
    return d;
255
};
256
257
258
App.repairLon = function (x, d) {
259
    'use strict';
260
261
    if (Coordinates.validLng(x)) {
262
        return x;
263
    }
264
265
    return d;
266
};
267
268
269
App.repairRadius = function (x, d) {
270
    'use strict';
271
272
    if (x === null || isNaN(x) || x < 0 || x > 100000000) {
273
        return d;
274
    }
275
276
    return x;
277
};
278
279
280
App.repairZoom = function (x, d) {
281
    'use strict';
282
283
    if (x === undefined || x === null || isNaN(x) || x < 1 || x > 20) {
284
        return d;
285
    }
286
287
    return x;
288
};
289
290
291
App.repairMaptype = function (t, d) {
292
    'use strict';
293
294
    var mapTypes = {
295
        "OSM": 1,
296
        "OSM/DE": 1,
297
        "OCM": 1,
298
        "OUTD": 1,
299
        "TOPO": 1,
300
        "satellite": 1,
301
        "hybrid": 1,
302
        "roadmap": 1,
303
        "terrain": 1
304
    };
305
306
    if (t !== undefined && mapTypes[t]) {
307
        return t;
308
    }
309
310
    return d;
311
};
312
313
314
App.parseMarkersFromUrl = function (urlarg) {
315
    'use strict';
316
317
    if (!urlarg) {
318
        return [];
319
    }
320
321
    var data;
322
323
    // ID:COODRS:R(:NAME)?|ID:COORDS:R(:NAME)?
324
    // COORDS=LAT:LON or DEG or DMMM
325
    if (urlarg.indexOf("*") >= 0) {
326
        data = urlarg.split('*');
327
    } else {
328
        /* sep is '|' */
329
        data = urlarg.split('|');
330
    }
331
332
    return data.map(function (dataitem) {
333
        dataitem = dataitem.split(':');
334
        if (dataitem.length < 3 || dataitem.length > 6) {
335
            return null;
336
        }
337
338
        var m = {
339
                alpha: dataitem[0],
340
                id: alpha2id(dataitem[0]),
341
                name: null,
342
                coords: null,
343
                r: 0,
344
                color: ""
345
            },
346
            index = 1,
347
            lat,
348
            lon;
349
350
        if (m.id < 0) {
351
            return null;
352
        }
353
354
        lat = parseFloat(dataitem[index]);
355
        lon = parseFloat(dataitem[index + 1]);
356
        if (Coordinates.valid(lat, lon)) {
357
            index += 2;
358
            m.coords = new google.maps.LatLng(lat, lon);
359
        } else {
360
            m.coords = Coordinates.fromString(dataitem[index]);
361
            index += 1;
362
        }
363
        if (!m.coords) {
364
            return null;
365
        }
366
367
        m.r = App.repairRadius(parseFloat(dataitem[index]), 0);
368
        index = index + 1;
369
370
        if (index < dataitem.length &&
371
                (/^([a-zA-Z0-9\-_]*)$/).test(dataitem[index])) {
372
            m.name = dataitem[index];
373
        }
374
375
        index = index + 1;
376
        if (index < dataitem.length &&
377
                (/^([a-fA-F0-9]{6})$/).test(dataitem[index])) {
378
            m.color = dataitem[index];
379
        }
380
381
        return m;
382
    }).filter(function (thing) {
383
        return thing !== null;
384
    });
385
};
386
387
388
App.parseCenterFromUrl = function (urlarg) {
389
    'use strict';
390
391
    if (!urlarg) {
392
        return null;
393
    }
394
395
    var data = urlarg.split(':');
396
397
    if (data.length === 1) {
398
        return Coordinates.fromString(data[0]);
399
    }
400
401
    if (data.length === 2) {
402
        return Coordinates.toLatLng(parseFloat(data[0]), parseFloat(data[1]));
403
    }
404
405
    return null;
406
};
407
408
409
App.parseLinesFromUrl = function (urlarg) {
410
    'use strict';
411
412
    if (!urlarg) {
413
        return [];
414
    }
415
416
    /* be backwards compatible */
417
    if (urlarg.length === 3
418
            && alpha2id(urlarg[0]) >= 0
419
            && urlarg[1] === '*'
420
            && alpha2id(urlarg[1]) >= 0) {
421
        urlarg = urlarg[0] + ':' + urlarg[2];
422
    }
423
424
    return urlarg.split('*').map(function (pair_string) {
425
        var pair = pair_string.split(':');
426
        if (pair.length === 2) {
427
            return {source: alpha2id(pair[0]), target: alpha2id(pair[1])};
428
        }
429
        return null;
430
    }).filter(function (thing) {
431
        return (thing !== null);
432
    });
433
};
434
435
436
App.parseMarkersFromCookies = function () {
437
    'use strict';
438
439
    var raw_ids = Cookies.get('markers');
440
441
    if (!raw_ids) {
442
        return [];
443
    }
444
445
    return raw_ids.split(':').map(function (id_string) {
446
        var m = {id: null, name: null, coords: null, r: 0, color: ""},
447
            raw_data,
448
            data;
449
450
        m.id = Conversion.getInteger(id_string, 0, 26 * 10);
451
        if (m.id === null) {
452
            return null;
453
        }
454
455
        raw_data = Cookies.get('marker' + m.id);
456
        if (!raw_data) {
457
            return null;
458
        }
459
460
        data = raw_data.split(':');
461
        if (data.length !== 4 && data.length !== 5) {
462
            return null;
463
        }
464
465
        m.coords = Coordinates.toLatLng(parseFloat(data[0]), parseFloat(data[1]));
466
        if (!m.coords) {
467
            return null;
468
        }
469
470
        m.r = App.repairRadius(parseFloat(data[2]), 0);
471
472
        if ((/^([a-zA-Z0-9\-_]*)$/).test(data[3])) {
473
            m.name = data[3];
474
        }
475
476
        if (data.length === 5 && (/^([a-fA-F0-9]{6})$/).test(data[4])) {
477
            m.color = data[4];
478
        }
479
480
        return m;
481
    }).filter(function (thing) {
482
        return (thing !== null);
483
    });
484
};
485
486
487
App.parseLinesFromCookies = function () {
488
    'use strict';
489
490
    var raw_lines = Cookies.get('lines');
491
492
    if (!raw_lines) {
493
        return [];
494
    }
495
496
    return raw_lines.split('*').map(function (pair_string) {
497
        var pair = pair_string.split(':');
498
        if (pair.length === 2) {
499
            return {source: alpha2id(pair[0]), target: alpha2id(pair[1])};
500
        }
501
        return null;
502
    }).filter(function (thing) {
503
        return (thing !== null);
504
    });
505
};
506
507
508
App.createMap = function (id, center, zoom, maptype) {
509
    'use strict';
510
511
    var m = new google.maps.Map(
512
        document.getElementById(id),
513
        {
514
            zoom: zoom,
515
            center: center,
516
            scaleControl: true,
517
            streetViewControl: false,
518
            mapTypeControlOptions: {
519
                mapTypeIds: ['OSM', 'OSM/DE', 'OCM', 'OUTD', 'TOPO', google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN]
520
            },
521
            mapTypeId: google.maps.MapTypeId.ROADMAP
522
        }
523
    );
524
525
    m.mapTypes.set("OSM", osmProvider("OSM"));
526
    m.mapTypes.set("OSM/DE", osmDeProvider("OSM/DE"));
527
    m.mapTypes.set("OCM", thunderforestProvider("OCM", "cycle", API_KEY_THUNDERFOREST));
528
    m.mapTypes.set("OUTD", thunderforestProvider("OUTD", "outdoors", API_KEY_THUNDERFOREST));
529
    m.mapTypes.set("TOPO", opentopomapProvider("TOPO"));
530
    m.setMapTypeId(maptype);
531
532
    Attribution.init(m);
533
    Sidebar.init(m);
534
    ExternalLinks.init(m);
535
    Markers.init(m);
536
    Lines.init(m);
537
    Geolocation.init(m);
538
    Hillshading.init(m);
539
    NPA.init(m);
540
    CDDA.init(m);
541
    Freifunk.init(m);
542
    Okapi.init(m);
543
    DownloadGPX.init(m);
544
545
    google.maps.event.addListener(m, "center_changed", function () {
546
        App.storeZoom();
547
        App.storeCenter();
548
    });
549
    google.maps.event.addListener(m, "zoom_changed", function () {
550
        App.storeZoom();
551
        App.storeCenter();
552
    });
553
    google.maps.event.addListener(m, "maptypeid_changed", function () {
554
        App.storeMapType();
555
    });
556
557
    Attribution.forceUpdate();
558
559
    return m;
560
};
561
562
563
$(document).ready(function () {
564
    'use strict';
565
    App.init();
566
});
567